/*
 * @(#)BabyAge.java	 Feb 24, 2012
 *
 * Copyright 2004-2012 WXXR Network Technology Co. Ltd. 
 * All rights reserved.
 * 
 * WXXR PROPRIETARY/CONFIDENTIAL.
 */

package com.ykdl.app.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 计算宝宝天数的工具类
 */
public class DateUtil {

	private static final int TIMEMILLIS_OF_DAY = 86400000;

	public static int getDays(long time1, long time2) {
		Date date1 = new Date(time1);
		Date date2 = new Date(time2);

		date1 = new Date(date1.getYear(), date1.getMonth(), date1.getDate());
		date2 = new Date(date2.getYear(), date2.getMonth(), date2.getDate());

		return (int) ((date1.getTime() - date2.getTime()) / TIMEMILLIS_OF_DAY);
	}

	public static CharSequence getTipAge(String age) {
		String tip = "";
		if (age != null && age.length() > 0) {
			int ageint = Integer.parseInt(age);
			int year = ageint / 372;
			int month = ageint % 372 / 31;
			// if (year > 0) {
			tip = year + "年";
			// }
			// if (month > 0) {
			tip = tip + month + "个月";
			// }
		}

		// if(tip.length() == 0){
		// tip = "0个月";
		// }
		return tip;
	}

	public static String getMiddleDate(String date) {
		if (date == null || date.length() == 0) {
			return "";
		}
		// date:2013-10-23 10:44:01
		String ndate = date.replace(" ", "-").replace(":", "-");
		String[] dates = ndate.split("-");
		Date d = new Date();
		if (!dates[0].equals(String.valueOf(d.getYear() + 1900))) {
			return dates[0] + "年" + dates[1] + "月" + dates[2] + "日";
		} else {
			long middletime = System.currentTimeMillis()
					- DateUtil.getMillsForIW(ndate);
			if (middletime > 24 * 60 * 60 * 1000) {
				long day = middletime / (24 * 60 * 60 * 1000L);
				return day + "天前";
			} else if (middletime > 1 * 60 * 60 * 1000) {
				long hour = middletime / (1 * 60 * 60 * 1000L);
				return hour + "小时前";
			} else if (middletime > 1 * 60 * 1000) {
				long minute = middletime / (1 * 60 * 1000L);
				return minute + "分钟前";
			} else if (middletime > 1 * 1000) {
				long second = middletime / (1 * 1000L);
				return second + "秒前";
			} else {
				return "0秒前";
			}
		}
	}

	public static String getBabyAge(long birthday, long current) {
		if (current < birthday) {
			return "";
		}

		int days = (int) ((current - birthday) / (1000 * 60 * 60 * 24));
		int year = days / 365;
		int month = (days % 365) / 30;
		int day = (days % 365) % 30;
		String result = null;
		if (year != 0 && month != 0 && day != 0) {
			result = (year == 0 ? "" : year + "岁")
					+ (month == 0 ? "" : month + "个月");
		} else {
			result = (year == 0 ? "" : year + "岁")
					+ (month == 0 ? "" : month + "个月")
					+ (day == 0 ? "" : day + "天");
		}
		if ("".equals(result)) {
			result = "0天";
		}
		return result;
	}

	public static String getBabyAge(String birthday, long current) {
		String age = "";
		try {

			int day = 0;
			int y = 0;
			int m = 0;
			int d = 0;

			if (birthday != null && birthday.length() <= 10) {
				String[] time = birthday.split("-");
				y = Integer.parseInt(time[0]);
				m = Integer.parseInt(time[1]);
				d = Integer.parseInt(time[2]);

				Calendar selectDate = Calendar.getInstance();
				Calendar currentDate = Calendar.getInstance();
				currentDate.setTime(new Date(current));
				selectDate.set(Calendar.YEAR, y);
				selectDate.set(Calendar.MONTH, m - 1);
				selectDate.set(Calendar.DAY_OF_MONTH, d);

				int selectMonth = currentDate.get(Calendar.MONTH);
				int selectYear = currentDate.get(Calendar.YEAR);
				int years = currentDate.get(Calendar.YEAR)
						- selectDate.get(Calendar.YEAR);
				int months = currentDate.get(Calendar.MONTH)
						- selectDate.get(Calendar.MONTH);
				int days = currentDate.get(Calendar.DAY_OF_MONTH)
						- selectDate.get(Calendar.DAY_OF_MONTH);
				if (selectMonth == 0)
					selectMonth = 12;
				// day = getDays(selectYear, selectMonth);
				day = 31;// 每个月都按31天来计算
				if (days < 0) {
					months = months - 1;
					days = days + day;
				}
				if (months < 0) {
					years = years - 1;
					months = months + 12;
				}
				if (years < 0) {
					years = 0;
				}

				int num = selectDate.get(Calendar.DATE);
				int tnum = currentDate.getActualMaximum(Calendar.DAY_OF_MONTH);
				int tnum1 = currentDate.get(Calendar.DATE);
				if (num >= day) {
					days = days + (num - day);
				}

				if (days == tnum && tnum1 == tnum) { // && num == day && days >=
														// tnum
					days = 0;
					months = months + 1;
				}
				days++;// 生下来，就算一天

				if (years == 0) {
					if (months == 0) {
						if (days == 1) {
							age = "宝宝出生了";
						} else {
							age = days + "天";
						}
					} else {
						if (days == 0) {
							age = months + "个月";
						} else {
							age = months + "个月" + days + "天";
						}
					}
				} else {
					if (months == 0) {
						if (days == 0) {
							age = years + "岁";
						} else {
							age = years + "岁" + days + "天";
						}
					} else {
						if (days == 0) {
							age = years + "岁" + months + "个月";
						} else {
							age = years + "岁" + months + "个月" + days + "天";
						}

					}
				}

			}
		} catch (Exception ee) {
			age = "";
		}
		return age;
	}

	/**
	 * 计算从 birthday，到current的天数
	 * 
	 * @param birthday
	 * @param current
	 * @return
	 */
	public static String getBabyDays2(String birthday, long current) {
		String age = "";
		int day = 0;
		int y = 0;
		int m = 0;
		int d = 0;

		if (birthday != null && birthday.length() <= 10) {
			String[] time = birthday.split("-");
			y = Integer.parseInt(time[0]);
			m = Integer.parseInt(time[1]);
			d = Integer.parseInt(time[2]);

			Calendar selectDate = Calendar.getInstance();
			Calendar currentDate = Calendar.getInstance();
			currentDate.setTime(new Date(current));
			selectDate.set(Calendar.YEAR, y);
			selectDate.set(Calendar.MONTH, m - 1);
			selectDate.set(Calendar.DAY_OF_MONTH, d);

			int selectMonth = currentDate.get(Calendar.MONTH);
			int selectYear = currentDate.get(Calendar.YEAR);
			int years = currentDate.get(Calendar.YEAR)
					- selectDate.get(Calendar.YEAR);
			int months = currentDate.get(Calendar.MONTH)
					- selectDate.get(Calendar.MONTH);
			int days = currentDate.get(Calendar.DAY_OF_MONTH)
					- selectDate.get(Calendar.DAY_OF_MONTH);
			if (selectMonth == 0)
				selectMonth = 12;
			// day = getDays(selectYear, selectMonth);
			day = 31;// 每个月都按31天来计算
			if (days < 0) {
				months = months - 1;
				days = days + day;
			}
			if (months < 0) {
				years = years - 1;
				months = months + 12;
			}
			if (years < 0) {
				years = 0;
			}

			int num = selectDate.get(Calendar.DATE);
			int tnum = currentDate.getActualMaximum(Calendar.DAY_OF_MONTH);
			int tnum1 = currentDate.get(Calendar.DATE);
			if (num >= day) {
				days = days + (num - day);
			}

			if (days == tnum && tnum1 == tnum) { // && num == day && days >=
													// tnum
				days = 0;
				months = months + 1;
			}
			days++;// 生下来，就算一天
			return String.valueOf(years * 372 + months * 31 + days);
		}

		return "0";

	}

	public static String getDayTip(String pdays) {
		if (pdays == null || pdays.length() == 0) {
			return "";
		}

		int days = Integer.parseInt(pdays);
		int year = days / 372;
		int month = days % 372 / 31;
		int day = days % 31;
		StringBuffer sb = new StringBuffer();
		if (year != 0) {
			sb.append(year).append("岁");
		}
		if (month != 0) {
			sb.append(month).append("个月");
		}

		if (day != 0) {
			sb.append(day).append("天");
		}

		return sb.toString();
	}

	public static final String DAY_LINK = "-";

	public static String getStrErvalDays(Calendar selectDate,
			Calendar currentDate) {
		int day = 0;

		int selectMonth = currentDate.get(Calendar.MONTH);
		int selectYear = currentDate.get(Calendar.YEAR);

		int years = currentDate.get(Calendar.YEAR)
				- selectDate.get(Calendar.YEAR);
		int months = currentDate.get(Calendar.MONTH)
				- selectDate.get(Calendar.MONTH);
		int days = currentDate.get(Calendar.DAY_OF_MONTH)
				- selectDate.get(Calendar.DAY_OF_MONTH);

		if (selectMonth == 0)
			selectMonth = 12;
		day = getDays(selectYear, selectMonth);

		if (days < 0) {
			months = months - 1;

			days = days + day;
		}
		if (months < 0) {
			years = years - 1;
			months = months + 12;
		}
		if (years < 0) {
			years = 0;
		}

		int num = selectDate.get(Calendar.DATE);
		int tnum = currentDate.getActualMaximum(Calendar.DAY_OF_MONTH);
		int tnum1 = currentDate.get(Calendar.DATE);
		if (num >= day) {
			days = days + (num - day);
		}

		if (days == tnum && tnum1 == tnum) { // && num == day && days >= tnum
			days = 0;
			months = months + 1;
		}
		return years + DAY_LINK + months + DAY_LINK + days;
	}

	/**
	 * 一年12月，每月31天来计算， 计算从 birthday，到现在的天数
	 * 
	 * @param birthday
	 * @return
	 */
	public static String getDays2(String birthday) {
		return getBabyDays2(birthday, System.currentTimeMillis());
	}

	/**
	 * 、 FIXME Comment this method desc：getDays得到某个月多少天
	 * 
	 * @param selectYear选择的年
	 * @param selectMonth选择的月
	 * @return int返回表示天数的int值
	 */
	public static int getDays(int selectYear, int selectMonth) {

		int day;
		switch (selectMonth) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			day = 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			day = 30;
			break;
		default:
			if (selectYear % 4 == 0 && selectYear % 100 != 0
					|| selectYear % 400 == 0) {
				day = 29;
			} else {
				day = 28;
			}
			break;
		}

		return day;

	}

	/*
	 * 得到前一天或后一天的数据
	 * 
	 * @param calendar 给定的日期
	 * 
	 * @param add 要得到的是前一天还是后一天
	 */
	public static Calendar nextday(Calendar calendar, boolean add) {
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		if (add) {
			// 取后一天
			if (month == 12 && day == 31) {
				year++;
				month = 1;
				day = 1;
			} else {
				if (day > 28) {
					if (getDays(year, month) == day) {
						day = 1;
						month++;
					} else {
						day++;
					}
				} else {
					day++;
				}
			}
		} else {
			if (month == 1 && day == 1) {
				year--;
				month = 12;
				day = 31;
			} else {
				if (day == 1) {
					day = getDays(year, month - 1);
					month--;
				} else {
					day--;
				}
			}
		}
		calendar.set(year, month - 1, day);
		return calendar;
	}

	/**
	 * 
	 * @param birthdate
	 * @return the baby age,formate: 1-1-2 year-month-day ,
	 */
	public static String getLongFormBabyBirthDay(String birthdate) {
		Calendar birth = Calendar.getInstance();
		String[] dates = birthdate.split("-");
		if (dates.length > 2) {
			birth.set(Integer.parseInt(dates[0]),
					Integer.parseInt(dates[1]) - 1, Integer.parseInt(dates[2]));
		}
		Calendar now = Calendar.getInstance();
		return getStrErvalDays(birth, now);
	}

	public static long getNotReadDay(String lastDayStr) {
		Date today = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		long noReadDays = 0;
		try {
			Date lastDay = sdf.parse(lastDayStr);
			noReadDays = (today.getTime() - lastDay.getTime())
					/ (24 * 60 * 60 * 1000);
		} catch (ParseException e) {
			// FIXME
			e.printStackTrace();
		}
		return noReadDays;

	}

	// 根据年，月，日，给出毫秒数
	public static long getMillisByDay(String month, String day, String hour,
			String minute) {
		Calendar birth = Calendar.getInstance();
		int year = birth.get(Calendar.YEAR);
		birth.set(year, Integer.parseInt(month) - 1, Integer.parseInt(day),
				Integer.parseInt(hour), Integer.parseInt(minute));
		return birth.getTimeInMillis();
	}

	// 根据年，月，日，给出毫秒数
	public static long getMillisByDay(String year, String month, String day,
			String hour, String minute) {
		Calendar birth = Calendar.getInstance();
		birth.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day), Integer.parseInt(hour),
				Integer.parseInt(minute));
		return birth.getTimeInMillis();
	}

	// 根据年，月，日，给出毫秒数,一天的开始时间
	public static long getMillisByDay(String year, String month, String day) {
		Calendar birth = Calendar.getInstance();
		birth.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day), 0, 0);
		return birth.getTimeInMillis();
	}

	// 根据年，月，日，给出毫秒数,一天的结束时间
	public static long getMaxMillisByDay(String year, String month, String day) {
		Calendar birth = Calendar.getInstance();
		birth.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day), 23, 59);
		return birth.getTimeInMillis();
	}

	// 根据开始时间，计算结束时间
	public static long getEndMillisByStart(long starttime, int minite) {
		return starttime + minite * 60 * 1000;
	}

	// 根据结束时间，计算开始时间
	public static long getStartMillisByEnd(long endtime, int minite) {
		return endtime - minite * 60 * 1000;
	}

	// 得到本周第一天的毫秒值
	public static long getMondayOfWeek() {
		int mondayPlus = getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus);
		Date monday = currentDate.getTime();

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String preMonday = df.format(monday);
		String[] dates = preMonday.split("-");
		return getMillisByDay(dates[0], dates[1], dates[2]);
	}

	// 得到本周第七天的毫秒值
	public static long getSundayOfWeek() {
		int mondayPlus = getMondayPlus() + 6;
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus);
		Date monday = currentDate.getTime();

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String preMonday = df.format(monday);
		String[] dates = preMonday.split("-");
		return getMaxMillisByDay(dates[0], dates[1], dates[2]);
	}

	public static long getMills(String date) {
		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));
		return lastDate.getTimeInMillis();
	}

	// i问
	public static long getMillsForIW(String date) {
		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split("-");
		int year = Integer.parseInt(dates[0]);
		int month = Integer.parseInt(dates[1]) - 1;
		int day = Integer.parseInt(dates[2]);
		int hour = Integer.parseInt(dates[3]);
		int minute = Integer.parseInt(dates[4]);
		int second = Integer.parseInt(dates[5]);
		lastDate.set(year, month, day, hour, minute, second);
		return lastDate.getTimeInMillis();
	}

	// 针对电子病历，服药时间picker
	public static long getMillsForFY(String date) {
		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split(",");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		String hour = dates[3];
		String minute = dates[4];
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day), Integer.parseInt(hour),
				Integer.parseInt(minute));
		return lastDate.getTimeInMillis();
	}

	public static long getMillsMin(String date) {
		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day), 0, 0, 0);
		return lastDate.getTimeInMillis();
	}

	public static long getMillsMax(String date) {
		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day), 23, 59, 59);
		return lastDate.getTimeInMillis();
	}

	// 得到本月第一天的毫秒值
	public static long getFirstDayOfMonth() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		str = sdf.format(lastDate.getTime());
		String[] dates = str.split("-");
		return getMillisByDay(dates[0], dates[1], dates[2]);
	}

	// 得到本年第一天的毫秒值
	public static long getCurrentYearFirst() {
		int yearPlus = getYearPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, yearPlus);
		Date yearDay = currentDate.getTime();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String preYearDay = df.format(yearDay);
		String[] dates = preYearDay.split("-");
		return getMillisByDay(dates[0], dates[1], dates[2]);
	}

	// 不许乱改了
	private static int getMondayPlus() {
		Calendar cd = Calendar.getInstance();
		// 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
		if (dayOfWeek == 0) {
			return -6;
		} else {
			return 1 - dayOfWeek;
		}
	}

	private static int getYearPlus() {
		Calendar cd = Calendar.getInstance();
		int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);// 获得当天是一年中的第几天
		cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期设为当年第一天
		cd.roll(Calendar.DAY_OF_YEAR, -1);// 把日期回滚一天。
		int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
		if (yearOfNumber == 1) {
			return -MaxYear;
		} else {
			return 1 - yearOfNumber;
		}
	}

	// 获取当月第一天
	public static String getFirstDayByMonth() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获取当月第一天
	public static String getFirstDayByMonth2() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获取当月第一天毫秒值
	public static long getFirstDayForMonth() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, 1);
		String date = sdf.format(cal.getTime());
		String[] dates = date.split("-");
		return getMillisByDay(dates[0], dates[1], dates[2]);
	}

	// 获取当月最后一天毫秒值
	public static long getEndDayForMonth() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, 0);
		cal.set(Calendar.DATE, 1);
		cal.roll(Calendar.DATE, -1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		String date = sdf.format(cal.getTime());
		String[] dates = date.split("-");
		return getMaxMillisByDay(dates[0], dates[1], dates[2]);
	}

	// 上月第一天的毫秒值
	public static long getPreviousMonthFirstDay(String date) {
		date = date.replace(".", "-");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		lastDate.add(Calendar.MONTH, -1);// 减一个月，变为下月的1号
		lastDate.set(Calendar.HOUR_OF_DAY, 0);
		lastDate.set(Calendar.MINUTE, 0);

		String str = sdf.format(lastDate.getTime());
		String[] dates1 = str.split("-");
		return getMillisByDay(dates1[0], dates1[1], dates1[2]);
	}

	// 上月最后一天的毫秒值
	public static long getPreviousMonthEndDay(String date) {
		date = date.replace(".", "-");
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));
		lastDate.add(Calendar.MONTH, -1);// 减一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		lastDate.roll(Calendar.DATE, -1);// 日期回滚一天，也就是本月最后一天
		lastDate.set(Calendar.HOUR_OF_DAY, 23);
		lastDate.set(Calendar.MINUTE, 59);
		str = sdf.format(lastDate.getTime());
		String[] dates1 = str.split("-");
		return getMaxMillisByDay(dates1[0], dates1[1], dates1[2]);
	}

	// 下个月第一天毫秒值
	public static long getNextMonthFirstDay(String date) {
		date = date.replace(".", "-");
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));
		lastDate.add(Calendar.MONTH, 1);// 减一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		lastDate.set(Calendar.HOUR_OF_DAY, 0);
		lastDate.set(Calendar.MINUTE, 0);
		str = sdf.format(lastDate.getTime());
		String[] dates1 = str.split("-");
		return getMillisByDay(dates1[0], dates1[1], dates1[2]);
	}

	// 下个月最后一天毫秒值
	public static long getNextMonthEndDay(String date) {
		date = date.replace(".", "-");
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));
		lastDate.add(Calendar.MONTH, 1);// 加一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		lastDate.roll(Calendar.DATE, -1);// 日期回滚一天，也就是本月最后一天
		lastDate.set(Calendar.HOUR_OF_DAY, 23);
		lastDate.set(Calendar.MINUTE, 59);
		str = sdf.format(lastDate.getTime());
		String[] dates1 = str.split("-");
		return getMaxMillisByDay(dates1[0], dates1[1], dates1[2]);
	}

	// 上月第一天
	public static String getPreviousMonthFirst(String date) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		lastDate.add(Calendar.MONTH, -1);// 减一个月，变为下月的1号
		lastDate.set(Calendar.HOUR_OF_DAY, 0);
		lastDate.set(Calendar.MINUTE, 0);
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获得上月最后一天的日期
	public static String getPreviousMonthEnd(String date) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));
		lastDate.add(Calendar.MONTH, -1);// 减一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		lastDate.roll(Calendar.DATE, -1);// 日期回滚一天，也就是本月最后一天
		lastDate.set(Calendar.HOUR_OF_DAY, 23);
		lastDate.set(Calendar.MINUTE, 59);
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 当前月第一天
	public static String getPreviousMonthFirst2(String date) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		lastDate.add(Calendar.MONTH, 0);// 减一个月，变为下月的1号
		lastDate.set(Calendar.HOUR_OF_DAY, 0);
		lastDate.set(Calendar.MINUTE, 0);
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获得当前月最后一天的日期
	public static String getPreviousMonthEnd2(String date) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));
		lastDate.add(Calendar.MONTH, 0);// 减一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		lastDate.roll(Calendar.DATE, -1);// 日期回滚一天，也就是本月最后一天
		lastDate.set(Calendar.HOUR_OF_DAY, 23);
		lastDate.set(Calendar.MINUTE, 59);
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获得下个月第一天的日期
	public static String getNextMonthFirst(String date) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));
		lastDate.add(Calendar.MONTH, 1);// 减一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		lastDate.set(Calendar.HOUR_OF_DAY, 0);
		lastDate.set(Calendar.MINUTE, 0);

		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获得下个月最后一天的日期
	public static String getNextMonthEnd(String date) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));
		lastDate.add(Calendar.MONTH, 1);// 加一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		lastDate.roll(Calendar.DATE, -1);// 日期回滚一天，也就是本月最后一天
		lastDate.set(Calendar.HOUR_OF_DAY, 23);
		lastDate.set(Calendar.MINUTE, 59);
		str = sdf.format(lastDate.getTime());
		return str;
	}

	public static String getDateData(float millis) {
		Date mdate = new Date((long) millis * 1000);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		int day = mdate.getDay();
		return format.format(mdate);
	}

	public static String getDateDataSting(String millis) {
		if (millis == null) {
			return "";
		} else {
			Date mdate = new Date(Math.round(Double.valueOf(millis)
					.doubleValue() * 1000));
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			String[] time = format.format(mdate).split("-");
			return time[0] + "年" + time[1] + "月" + time[2] + "日";
		}

	}

	public static String getDateDataLong(String millis) {
		Date mdate = new Date(Long.parseLong(millis));
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String[] time = format.format(mdate).split("-");
		return time[0] + "年" + time[1] + "月" + time[2] + "日";
	}

	public static String getDateData2(long millis) {
		Date mdate = new Date(millis);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(mdate);
	}

	public static String getDateDatatimeSting(String millis) {
		Date mdate = new Date(
				Math.round(Double.valueOf(millis).doubleValue() * 1000));
		SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH:mm");
		return format.format(mdate);
	}

	public static String getDateData3(long millis) {
		Date mdate = new Date(millis);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		int day = mdate.getDay();
		return format.format(mdate) + "-" + day;
	}

	public static String getWeek(String date) {

		switch (Integer.valueOf(date)) {
		case 1:
			return "星期一";
		case 2:
			return "星期二";
		case 3:
			return "星期三";
		case 4:
			return "星期四";
		case 5:
			return "星期五";
		case 6:
			return "星期六";
		case 0:
			return "星期日";
		}
		return "星期一";
	}

	public static String getYearAndMonth(String pdays) {
		try {
			int days = Integer.parseInt(pdays);
			String result = "";
			int year = days / 365;
			int month = days % 365 / 31;
			if (year != 0) {
				result += year + "岁";
			}
			if (month != 0) {
				result += month + " 月";
			}
			return result;
		} catch (Exception ee) {
			return "";
		}
	}

	public static String getTime0(String string) {
		// 2011年，1月1日，0时，0分钟，
		Date date = new Date();
		string = date.getYear() + 1900 + "年," + string;
		String[] array = string.split(",");
		Calendar cal = Calendar.getInstance();
		int year = Integer.parseInt(array[0].substring(0, 4));
		int month, day, hourOfDay, minute, second;

		hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
		minute = cal.get(Calendar.MINUTE);
		month = cal.get(Calendar.MONTH) + 1;
		day = cal.get(Calendar.DAY_OF_MONTH);
		second = cal.get(Calendar.SECOND);
		if (!array[1].equals("今天")) {
			month = getIntMonth(array[1]);
			day = getIntDay(array[1]);
		}

		hourOfDay = getInterValue(array[2]);
		minute = getInterValue(array[3]);

		return year + "-" + month + "-" + day + " " + hourOfDay + ":" + minute;
	}

	public static int getIntDay(String monthday) {
		int index = monthday.indexOf("月");
		return Integer.parseInt(monthday.substring(index + 1,
				monthday.length() - 1));
	}

	public static int getIntMonth(String monthday) {
		int index = monthday.indexOf("月");
		return Integer.parseInt(monthday.substring(0, index));
	}

	public static int getInterValue(String value) {
		int result = -1;
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < value.length(); i++) {
			if (value.charAt(i) < 256) {
				sb.append(value.charAt(i));
			} else {
				result = Integer.parseInt(sb.toString());
			}
		}
		return result;
	}

	// 当月第一天
	public static long getMonthFirstDayMillis(String date) {
		date = date.replace(".", "-");
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		lastDate.set(Calendar.HOUR_OF_DAY, 0);
		lastDate.set(Calendar.MINUTE, 0);
		str = sdf.format(lastDate.getTime());
		String[] dates1 = str.split("-");
		return getMillisByDay(dates1[0], dates1[1], dates1[2]);
	}

	// 当月最后一天
	public static long getMonthEndDayMillis(String date) {
		date = date.replace(".", "-");
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		String[] dates = date.split("-");
		String year = dates[0];
		String month = dates[1];
		String day = dates[2];
		lastDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				getDays(Integer.parseInt(year), Integer.parseInt(month)));

		str = sdf.format(lastDate.getTime());
		String[] dates1 = str.split("-");
		return getMaxMillisByDay(dates1[0], dates1[1], dates1[2]);
	}

	public static String getTopicDate(String date) {
		if (null == date) {
			return "";
		}
		Date mdate = new Date(
				Math.round(Double.valueOf(date).doubleValue() * 1000));
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// date:2013-10-23 10:44:01
		date = format.format(mdate);
		String ndate = date.replace(" ", "-").replace(":", "-");
		String[] dates = ndate.split("-");
		Date d = new Date();
		return dates[0] + "年" + dates[1] + "月" + dates[2] + "日";
	}

	public static String gettopicDate(String date) {
		Date mdate = new Date(
				Math.round(Double.valueOf(date).doubleValue() * 1000));
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// date:2013-10-23 10:44:01
		date = format.format(mdate);
		String ndate = date.replace(" ", "-").replace(":", "-");
		String[] dates = ndate.split("-");
		Date d = new Date();
		if (!dates[0].equals(String.valueOf(d.getYear() + 1900))) {
			return dates[0] + "年" + dates[1] + "月" + dates[2] + "日";
		} else {
			long middletime = System.currentTimeMillis()
					- DateUtil.getMillsForIW(ndate);
			if (middletime > 24 * 60 * 60 * 1000) {
				long day = middletime / (24 * 60 * 60 * 1000L);
				return day + "天前";
			} else if (middletime > 1 * 60 * 60 * 1000) {
				long hour = middletime / (1 * 60 * 60 * 1000L);
				return hour + "小时前";
			} else if (middletime > 1 * 60 * 1000) {
				long minute = middletime / (1 * 60 * 1000L);
				return minute + "分钟前";
			} else if (middletime > 1 * 1000) {
				long second = middletime / (1 * 1000L);
				return second + "秒前";
			} else {
				return "1秒前";
			}
		}
	}

	public static String gettimeDate(String date) {
		Date mdate = new Date(
				Math.round(Double.valueOf(date).doubleValue() * 1000));
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return format.format(mdate);
	}

	public static String getStrDate(float fdate) {
		Date date = new Date((long) fdate * 1000);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}

	public static String getNowDate() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}
	/**
	 * 判断时间是上午还是下午
	 * @param date
	 * @return 0 上午  1 下午
	 */
	public static int isAmOrPm(String date){
		Date mdate = new Date(
				Math.round(Double.valueOf(date).doubleValue() * 1000));
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(mdate);
		return calendar.get(GregorianCalendar.AM_PM);
	}
}
